<?php
/**
 * Created by PhpStorm.
 * User: gyccvvh
 * Date: 2018-06-14
 * Time: 17:38
 */

namespace app\home\controller;


use think\Db;

class Merchant extends Basic
{
    public function _initialize()
    {
        parent::_initialize();
        $arr[0]['text']='列表';
        $arr[0]['url']='';
        $this->assign('third_menu', $arr);
    }

    public function index() {
        $where = input('');
        $query = Db::table("sp_merchant");
        decorate_query($query,[
            ["username","like"],
            ["mobile","like"],
            ["email","like"],
            ["is_active"],
            ["status"],
            ["reg_time",">=","start"],
            ["reg_time","<=","end"]
        ],$where);
        $records = $query->order("reg_time desc")->paginate(15,false,[
            "query"=>$where
        ]);
        $this->assign("records",$records);
        $this->assign("where", $where);
        return $this->fetch();
    }

    public function del() {
        $id = input("id");
        $res = Db::table("sp_merchant")->where("id",$id)->delete();
        if($res) {
            return $this->ret->setCode(0)->setMsg("删除成功")->toJson();
        }else {
            return $this->ret->setCode(1)->setMsg("删除失败")->toJson();
        }
    }
    public function status() {
        $id = input("id");
        $status = (intval(input("status")) + 1)%2;
        $res = Db::table("sp_merchant")->where("id",$id)->update(["status"=>$status]);
        if($res) {
            return $this->ret->setCode(0)->setMsg("操作成功")->toJson();
        }else {
            return $this->ret->setCode(1)->setMsg("操作失败")->toJson();
        }
    }

    public function auth() {
        if($this->request->isPost()) {
            $id = input("id");
            $aso = input("aso/a");
            Db::startTrans();
            try{
                Db::table("sp_mer_auth_aso")->where("mer_id",$id)->delete();
                foreach ($aso as $a) {
                    Db::table("sp_mer_auth_aso")->insert(["mer_id"=>$id,"auth_id"=>$a]);
                }
                Db::commit();
                return $this->ret->setCode(0)->setMsg("修改成功")->toJson();
            }catch (\Exception $e) {
                Db::rollback();
                return $this->ret->setCode(1)->setMsg("修改失败")->toJson();
            }

        }else {
            $id = input('id');
            $auths = Db::table("sp_mer_auth")->select();
            $auth_aso = Db::table("sp_mer_auth_aso")->where("mer_id",$id)->field("auth_id")->select();
            $aso = pick_from_2array("auth_id",$auth_aso);
            foreach($auths as $key => $auth) {
                if(in_array($auth["id"],$aso)){
                    $auths[$key]["checked"] = "true";
                }else {
                    $auths[$key]["checked"] = false;
                }
            }
            $this->assign("auths", $auths);
            return $this->fetch();
        }
    }

    /*
     * 店铺管理
     */
    public function mershop(){
        $where = input('');
        $query = Db::table("sp_mer_info")->alias("mi")
            ->join(["sp_merchant","m"],"m.id = mi.mer_id","LEFT")
            ->join(["sp_area","a1"],"a1.id = mi.mer_province","LEFT")
            ->join(["sp_area","a2"],"a2.id = mi.mer_city","LEFT")
            ->join(["sp_area","a3"],"a3.id = mi.mer_area","LEFT")
            ->join(["sp_mer_level" , "l"],"l.id = mi.level","LEFT")
            ->field("m.username,m.mobile,m.email,m.is_active,mi.*,a1.name as province,a2.name as city,a3.name as area,l.level_name");
        decorate_query($query,[
            ["username","like"],
            ["mobile","like"],
            ["email","like"],
            ["mer_name","like"],
            ["contact_user","like"],
            ["is_active"],
            ["aptitude1"],
            ["aptitude2"],
            ["aptitude3"]
        ],$where);
        $records = $query->paginate(15,false,[
            "query" => $where
        ]);
        $this->assign("where", $where);
        $this->assign("records",$records);
        return $this->fetch();
    }


    public function audit_cert(){

    }
    public function audit_deposit() {

    }

    public function audit_store() {

    }

}